'use strict';

/**
 * @param {string} s
 * @param {number} numRows
 * @return {string}
 */
const convert = function(s, numRows) {
  const arrays = [];
  for (let i = 0; i < numRows; i++) {
    arrays.push([]);
  }

  let dir = true;
  const nextIdx = (i) => {
    if (dir) {
      if (i < numRows - 1) {
        i = i + 1;
      }
      if (i >= numRows - 1) {
        dir = false;
      }
    } else {
      if (i > 0) {
        i = i - 1;
      }
      if (i <= 0) {
        dir = true;
      }
    }
    return i;
  }
  let idx = 0;
  for (const c of s.split('')) {
    const arr = arrays[idx];
    idx = nextIdx(idx);
    arr.push(c);

  }
  const result = [];
  for (const arr of arrays) {
    result.push(...arr);
  }
  return result.join('');
};
